package com.rabbiter.hwm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbiter.hwm.common.Result;
import com.rabbiter.hwm.controller.dto.StudentDTO;
import com.rabbiter.hwm.entity.StudentFile;
import com.rabbiter.hwm.mapper.StudentFileMapper;
import com.rabbiter.hwm.service.IAutoCheckService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 自动批改控制器
 */
@RestController
@RequestMapping("/auto-check")
public class AutoCheckController {

    @Resource
    private IAutoCheckService autoCheckService;

    @Resource
    private StudentFileMapper studentFileMapper;

    /**
     * 为作业设置参考答案关键词
     * @param params 包含作业ID和关键词的参数
     * @return 操作结果
     */
    @PostMapping("/set-keywords")
    public Result setAnswerKeywords(@RequestBody Map<String, Object> params) {
        try {
            Integer userFileId = Integer.parseInt(params.get("userFileId").toString());
            String keywords = params.get("keywords").toString();
            autoCheckService.setAnswerKeywords(userFileId, keywords);
            return Result.success("关键词设置成功");
        } catch (Exception e) {
            return Result.error("关键词设置失败: " + e.getMessage());
        }
    }

    /**
     * 获取作业的参考答案关键词
     * @param userFileId 作业ID
     * @return 关键词信息
     */
    @GetMapping("/get-keywords/{userFileId}")
    public Result getAnswerKeywords(@PathVariable Integer userFileId) {
        try {
            String keywords = autoCheckService.getAnswerKeywords(userFileId);
            Map<String, Object> result = new HashMap<>();
            result.put("userFileId", userFileId);
            result.put("keywords", keywords);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("获取关键词失败: " + e.getMessage());
        }
    }

    /**
     * 自动批改单个作业
     * @param studentFileId 学生作业ID
     * @return 批改结果
     */
    @GetMapping("/single/{studentFileId}")
    public Result autoCheckSingleHomework(@PathVariable Integer studentFileId) {
        try {
            // 查询学生作业信息
            StudentFile studentFile = studentFileMapper.selectById(studentFileId);
            if (studentFile == null) {
                return Result.error("找不到该作业");
            }

            // 执行自动批改
            StudentDTO result = autoCheckService.autoCheckHomework(studentFile);

            // 更新数据库
            studentFile.setFlag(result.getFlag());
            studentFile.setScore(result.getScore());
            studentFileMapper.updateById(studentFile);

            return Result.success(result);
        } catch (Exception e) {
            return Result.error("自动批改失败: " + e.getMessage());
        }
    }

    /**
     * 批量自动批改某个作业的所有提交
     * @param userFileId 作业ID
     * @return 批改结果
     */
    @GetMapping("/batch/{userFileId}")
    public Result batchAutoCheckHomework(@PathVariable Integer userFileId) {
        try {
            // 执行批量自动批改
            int checkedCount = autoCheckService.batchAutoCheckHomework(userFileId);
            return Result.success("批量自动批改完成，共批改了" + checkedCount + "份作业");
        } catch (Exception e) {
            return Result.error("批量自动批改失败: " + e.getMessage());
        }
    }
}